#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        if ((s[i - 1] == '0' && s[i] == '1') || (s[i - 1] == '1' && s[i] == '0'))
            cnt++;
    }
    if (cnt >= 3) cout << 0 << endl;
    else if (cnt == 0) cout << 2 << endl;
    else if (cnt == 1)
    {
        if (n == 4)
        {
            if (s == "1100" || s == "0011") cout << 2 << endl;
            else cout << 1 << endl;
        }
        else cout << 1 << endl;
    }
    else
    {
        if (s == "0110" || s == "1001") cout << 2 << endl;
        else cout << 1 << endl;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}